/**
 * Created by tongjunshui on 17/6/8.
 */
var express = require('express');
var request = require('request');
var router = express.Router();


var dbDao=require('../models/dbBase.js');


router.get("/",function(req,res){
    /*首页---jstong*/
    var code = req.query.code;
    console.log('---/---code:',code);
    var openid='';

    if(code){
        oauth_client.getAccessToken(code, function (err, result) {
            console.log(result)
            if(err){

                res.render("error.html");
            }else{
                var accessToken = result.data.access_token;
                openid = result.data.openid;
                console.log('---/---openid:',openid);
                oauth_client.getUser(openid, function (err, result) {
                    if(err){
                        console.log('getUser err:',err);
                    }else{
                        console.log('---/---userinfo:',result);
                        //查找该用户信息
                        checkIfNewUser(result);
                    }

                });
                res.render("index.html",{hyGym_wxopenid:openid});
            }

        });
    }else{
        //console.log('rendersss')
        res.render("index.html",{hyGym_wxopenid:openid});
    }

});
function checkIfNewUser(userinfo){
    var sql = "select * from members where wxopenId='" + userinfo.openid + "'";
    var obj = userinfo;
    var sex = {"1":"男","2":"女","3":"未知"};
    dbDao.find( sql, function (err, result2) {
        if(result2.length>0){
            console.log("老微信一键登陆用户", result2[0]);
        }else{
            var params=[
                "wx_" + obj.openid,obj.openid,obj.nickname,obj.headimgurl, obj.province, obj.city,
                '微信',sex[obj.sex],'已关注',obj.province + " " + obj.city,null
            ];
            var sql="insert into members(account,wxopenId,nickName,headimgurl,province,city,source,sex,status,address,phone) values(?,?,?,?,?,?,?,?,?,?,?)";
            dbDao.querys(sql,params,function(err,result){
                if(result.resultCode=='0'){
                    console.log('添加用户成功');
                }else{
                    console.log('添加用户失败');
                }
            });
        }
    });
}
router.get("/myaccount",function(req,res){
    /*首页---jstong*/
    var code = req.query.code;
    console.log('---/---code:',code);
    var openid='';

    if(code){
        oauth_client.getAccessToken(code, function (err, result) {
            console.log(result)
            if(err){

                res.render("error.html");
            }else {
                var accessToken = result.data.access_token;
                openid = result.data.openid;

                console.log('---/---openid:',openid);
                oauth_client.getUser(openid, function (err, result) {

                    //查找该用户信息
                    if(err){
                        console.log('getUser err:',err);
                    }else{
                        console.log('---/---userinfo:',result);
                        //查找该用户信息
                        checkIfNewUser(result);
                    }
                });
                res.render("index.html",{hyGym_wxopenid:openid});
            }
        });
    }else{
        //console.log('rendersss')
        res.render("index.html",{hyGym_wxopenid:openid});
    }

});

module.exports = router;
